package it.blueocean.acanto.taglibrary.jsp.taglib.tags.commands;

import it.blueocean.acanto.taglibrary.jsp.taglib.tags.AbstractComponentStateBean;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.AbstractUIComponentTag;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.utils.HttpUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.log4j.Logger;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

public class SetComponentsPropertiesTag extends AbstractUIComponentTag
{
	private static final long serialVersionUID = 7142004684451852882L;

	private static final Logger logger_ = Logger.getLogger(SetComponentsPropertiesTag.class);

	protected Map properties_ = new HashMap();

	private boolean ajax_ = false;

	private String ajaxExpr_;

	private String propertyNameExpr_ = null;

	private String propertyValueExpr_ = null;

	private String nextUrl_;

	private String nextUrlExpr_;

	public void setGoto(String paramString)
	{
		this.nextUrlExpr_ = paramString;
	}

	public String getGoto()
	{
		return this.nextUrl_;
	}

	public String getAjax()
	{
		return this.ajaxExpr_;
	}

	public boolean isAjaxEnabled()
	{
		return this.ajax_;
	}

	public void setAjax(String paramString)
	{
		this.ajaxExpr_ = paramString;
	}

	public void setProperty(String paramString)
	{
		this.propertyNameExpr_ = paramString;
	}

	public void setValue(String paramString)
	{
		this.propertyValueExpr_ = paramString;
	}

	public int doStartTag(AbstractComponentStateBean paramAbstractComponentStateBean) throws JspException
	{
		evaluateExpressions();
		return 2;
	}

	public int doEndTag(AbstractComponentStateBean paramAbstractComponentStateBean) throws JspException
	{
		JspWriter localJspWriter = this.pageContext.getOut();
		try
		{
			localJspWriter.print(getUrl());
		}
		catch (IOException localIOException)
		{
			logger_.error(localIOException);
		}
		return 6;
	}

	protected String getUrl() throws UnsupportedEncodingException
	{
		return HttpUtils.generateSetPropertyURL(this.properties_, this.nextUrl_,
				(HttpServletRequest) this.pageContext.getRequest(), this);
	}
	
	protected void evaluateExpressions() throws JspException
	{
		properties_ = new HashMap();
		if(propertyNameExpr_ != null && propertyValueExpr_ != null)
		{
			String s = (String) ExpressionEvaluatorManager.evaluate("property", propertyNameExpr_,
					String.class, this, pageContext);
			String s1 = (String) ExpressionEvaluatorManager.evaluate("value", propertyValueExpr_, String.class,
					this, pageContext);
			properties_.put(s, s1);
		}
		if(nextUrlExpr_ != null)
			nextUrl_ = (String) ExpressionEvaluatorManager.evaluate("goto", nextUrlExpr_, String.class, this,
					pageContext);
		else
			nextUrl_ = HttpUtils.getPageUrl((HttpServletRequest) pageContext.getRequest());
		if(ajaxExpr_ != null)
		{
			Object obj = ExpressionEvaluatorManager
					.evaluate("ajax", ajaxExpr_, Object.class, this, pageContext);
			if(obj instanceof Boolean)
				ajax_ = ((Boolean) obj).booleanValue();
			else
				ajax_ = Boolean.valueOf(obj.toString()).booleanValue();
			if(ajax_)
				properties_.put("ajax", "true");
		}
	}

	public void setProperty(String paramString1, String paramString2)
	{
		this.properties_.put(paramString1, paramString2);
	}

	public Map getProperties()
	{
		return this.properties_;
	}

	public AbstractComponentStateBean initializeState() throws JspException
	{
		return new FooStateBean();
	}

}
